using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Sec_scan漏洞扫描系统.Sec_Service
{
    /// <summary>
    /// 获取WEB信息公共方法
    /// </summary>
    public class Sec_WebInfoScan_Secvice
    {
        /// <summary>
        /// 获取网站标题
        /// </summary>
        /// <param name="title"></param>
        /// <returns></returns>
        public static string GranWebTitle(string title)
        {
            try
            {
                string url = Sec_Api.Sec_webInfoScan_Secvice_Api.Url;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader red = new StreamReader(response.GetResponseStream());
                string html = red.ReadToEnd();
                Match m1 = Regex.Match(html, "<title>(.*)</title>");
                Sec_Api.Sec_webInfoScan_Secvice_Api.Title = m1.Groups[1].Value;
            }
            catch(Exception ex)
            {
                string Exp = ex.ToString();
                Sec_Api.Sec_webInfoScan_Secvice_Api.OptionsExp.Append(Exp.ToString());
            }
            return title;
        }
        /// <summary>
        /// 获取网站服务名称
        /// </summary>
        /// <param name="server"></param>
        /// <returns></returns>
        public static string GranService(string server)
        {
            try
            {
                string url = Sec_Api.Sec_webInfoScan_Secvice_Api.Url;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "GET";
                request.AllowAutoRedirect = false;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string cc = response.GetResponseHeader("Server");//解析网站服务名称
                Sec_Api.Sec_webInfoScan_Secvice_Api.ServerName = cc.ToString();
            }
            catch(Exception ex)
            {
                string Exp = ex.ToString();
                Sec_Api.Sec_webInfoScan_Secvice_Api.OptionsExp.Append(Exp.ToString());
            }
            return server;
        }
        /// <summary>
        /// 获取IP地址信息
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static string GranIpInfo(string ip)
        {
            try
            {
                string url = Sec_Api.Sec_webInfoScan_Secvice_Api.Url;
                UriBuilder ub = new UriBuilder(url);
                string host = (ub.Host.Replace("https://", "http://"));
                IPAddress[] addresses = Dns.GetHostAddresses(host);
                foreach(IPAddress threaddress in addresses)
                {
                    Sec_Api.Sec_webInfoScan_Secvice_Api.IpInfo = threaddress.ToString();
                }
            }
            catch(Exception ex)
            {
                string Exp = ex.ToString();
                Sec_Api.Sec_webInfoScan_Secvice_Api.OptionsExp.Append(Exp.ToString());
            }
            return ip;
        }
    }
}
